package study.db.postgresql.service;

import cn.hutool.core.util.PageUtil;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import study.db.postgresql.mapper.examenrolluser.ExamEnrollUserEO;
import study.db.postgresql.mapper.examenrolluser.ExamEnrollUserMapper;
import study.db.postgresql.mapper.examenrolluserext.ExamEnrollUserExtEO;
import study.db.postgresql.mapper.examenrolluserext.ExamEnrollUserExtMapper;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j(topic = "MY_LOG")
@Service
public class DataRefactorService {

    private int pageSize = 100;

    @Autowired
    private ExamEnrollUserMapper examEnrollUserMapper;
    @Autowired
    private ExamEnrollUserExtMapper examEnrollUserExtMapper;

    /**
     * 全量
     * （1）删除目标表
     * （2）插入目标表
     */
    public void doRefactorByTotal() {
        // 分页
        int total = examEnrollUserMapper.getTotal();
        if (total == 0) {
            log.info("");
            return;
        }
        int page = PageUtil.totalPage(total, pageSize);
        log.info(">>>>>> 数据 {} 条, 每页 {} 条, 共计 {} 页", total, pageSize, page);

        // 遍历
        for (int pageNo = 0; pageNo < page; pageNo++) {
            // 源表
            PageHelper.startPage(pageNo, pageSize);
            List<ExamEnrollUserEO> userEOLt = examEnrollUserMapper.getEnrollUserLt();
            List<Long> enrollIdLt = userEOLt.stream()
                    .map(ExamEnrollUserEO::getId).collect(Collectors.toList());

            // 清除目标表
            int size = examEnrollUserExtMapper.delByEnrollIds(enrollIdLt);
            log.info(">>>>>> 删除 {}", size);
            // 重组数据
            List<ExamEnrollUserExtEO> extEOLt = userEOLt.stream()
                    .map(DataRefactorHelper::splitBySubject)
                    .flatMap(List::stream)
                    .collect(Collectors.toList());
            // 插入目标表
            log.info(">>>>>> 插入 {}", extEOLt.size());
            Lists.partition(extEOLt, 50).forEach(examEnrollUserExtMapper::insertByBatch);
        }
    }

    /**
     * 增量
     * （1）
     * （2）
     */
    public void doRefactorByIncr() {
        // 分页
        int total = examEnrollUserMapper.getTotal();
        if (total == 0) {
            log.info("");
            return;
        }
        int page = PageUtil.totalPage(total, pageSize);
        log.info(">>>>>> 每页 {} 条, 共计 {} 页", pageSize, page);

        // 遍历
        for (int pageNo = 0; pageNo < 1; pageNo++) {
            // 源表
            PageHelper.startPage(pageNo, pageSize);
            List<ExamEnrollUserEO> userEOLt = examEnrollUserMapper.getEnrollUserLt();

            // 目标表
            List<ExamEnrollUserExtEO> extEOLt = userEOLt.stream()
                    .map(DataRefactorHelper::splitBySubject)
                    .flatMap(List::stream)
                    .collect(Collectors.toList());

            //
            log.info(">>>> {}", extEOLt.size());
            Lists.partition(extEOLt, 50).forEach(examEnrollUserExtMapper::insertByBatch);
//            extEOLt.forEach(e -> {
////                log.info("{}", e);
//            });
        }
    }


    public static void main(String[] args) {
        int page = PageUtil.totalPage(100, 30);
        log.info("{}", page);
    }
}
